package top.yiwyn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import top.yiwyn.common.ResultBean;
import top.yiwyn.domain.*;
import top.yiwyn.mapper.ArcticleMapper;
import top.yiwyn.mapper.ArcticleTagMapper;
import top.yiwyn.mapper.LoggerMapper;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/arcticle")
public class ArcticleController {


    @Autowired
    ArcticleMapper arcticleMapper;

    @Autowired
    ArcticleTagMapper arcticleTagMapper;

    @Autowired
    LoggerMapper loggerMapper;


    /**
     * 根据id获取需要的文章信息
     * 取得的是所有的信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getArcticleById/{id}")
    public Arcticle getArcticleById(@PathVariable("id") long id) {

        Arcticle arcticle = arcticleMapper.selectById(id);
        return arcticle;
    }


    /**
     * 分页获取文章
     *
     * @return
     */
    @GetMapping("/getpagedata")
    public ResultBean getPageDta(int current) {

        Page<Arcticle> arcticlePage = new Page<>(current, 9);
        arcticleMapper.selectPage(arcticlePage);
        PageData pageData = new PageData(arcticlePage.getPages(), arcticlePage.getTotal());

        return ResultBean.succ("获取成功", pageData);
    }


    @GetMapping("/getArcticlePage")
    public List<Arcticle> getArcticlePage(int current) {
        Page<Arcticle> arcticlePage = new Page<>(current, 9);
        IPage<Arcticle> arcticleIPage = arcticleMapper.selectPage(arcticlePage);
        List<Arcticle> records = arcticleIPage.getRecords();
        return records;
    }


    /**
     * 根据id删除文章
     *
     * @param id
     */
    @Transactional
    @DeleteMapping("/deletearcticle/{id}")
    public ResultBean deleteById(@PathVariable("id") String id) {
        String title = arcticleMapper.getArcticleTitleFromId(id);
        loggerMapper.insert(new LogEntity("删除新文章:  " + title, "red"));
        arcticleMapper.deleteById(id);
        arcticleTagMapper.delete(new QueryWrapper<ArcticleTag>().eq("tag_id", id));
        return ResultBean.succ("删除成功", null);
    }


    /**
     * 添加文章
     * <form action="http://localhost:8081/arcticle/addarcticle" method="post">
     * <input type="text" name="title" />
     * <input type="text" name="content" />
     * <input type="text" name="imgurl" />
     *
     * <input type="checkbox" name="taglist" value=1 />
     * <input type="checkbox" name="taglist" value=2 />
     * <input type="checkbox" name="taglist" value=3 />
     *
     * <input type="submit" value="提交" />
     * </form>
     *
     * @param arcticle
     */
    @Transactional
    @PostMapping("/addarcticle")
    public ResultBean addArcticle(Arcticle arcticle, int[] taglist) {

        arcticle.setCreateDate(new Date());

        arcticleMapper.insert(arcticle);

        for (int i : taglist) {
            arcticleTagMapper.insert(new ArcticleTag(arcticle.getId(), i));
        }

        loggerMapper.insert(new LogEntity("添加新文章:  " + arcticle.getTitle(),"green"));
        return ResultBean.succ("添加成功", null);
    }


    /**
     * 这里需要的 arcticle 可以使用id获取
     * 修改表达的提交action
     *
     * @param arcticle
     */
    @Transactional
    @PostMapping("/modify")
    public ResultBean modify(Arcticle arcticle, int[] taglist) {
        arcticle.setModifyDate(new Date());
        arcticleMapper.updateById(arcticle);


        int id = arcticle.getId();

        String title = arcticleMapper.getArcticleTitleFromId(id + "");
        loggerMapper.insert(new LogEntity("修改了文章:  " + title,"yellow"));

        arcticleTagMapper.delete(new QueryWrapper<ArcticleTag>().eq("arcticle_id", id));

        for (int i : taglist) {
            arcticleTagMapper.insert(new ArcticleTag(arcticle.getId(), i));
        }

        return ResultBean.succ("修改成功", null);
    }


    /**
     * 根据id查询文章的所有tag
     *
     * @param id
     * @return
     */
    @GetMapping("/gettag/{id}")
    public List<Tag> getTag(@PathVariable("id") int id) {
        List<Tag> tags = arcticleMapper.selectAllTagById(id);
        return tags;
    }

}
